package light.head.dao.mobile;

import java.util.ArrayList;
import java.util.List;

import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.model.ApplyItem;

import org.springframework.stereotype.Service;

@Service
public class MobileGoodsApplyItemDAO extends BaseDAO<ApplyItem, Integer> {

	private static final String sql_findByApplyCode = "FROM ApplyItem a WHERE a.acode = ?";
	
	
	/**
	 * 通过申请信息编号获取所有申请明细
	 *@param ac	申请信息编号
	 *@author yangguanghe [2014-5-21_下午11:53:43]
	 */
	public List<ApplyItem> findByApplyCode (Integer ac) {
		if (null == ac) return null;
		return find(sql_findByApplyCode, ac);
	}
	
	/**
	 * 通过申请信息编号集合获取所有申请明细
	 *@param acs 申请信息编号
	 *@author yangguanghe [2014-5-21_下午11:52:42]
	 */
	public List<ApplyItem> findByApplyCode (List<Integer> acs) {
		if (null == acs || acs.size() == 0) return null;
		
		StringBuffer hql = new StringBuffer("FROM ApplyItem a WHERE a.acode IN (");
		List<Object> parm = new ArrayList<Object>();
		
		for (Integer i : acs) {
			if (null != i) {
				hql.append("?,");
				parm.add(i);
			}
		}
		
		hql.deleteCharAt(hql.length() - 1).append(")");
		return find(hql.toString(), parm.toArray());
	}
	
	
	public RESULT update (ApplyItem item) {
		if (null == item || null == item.getCode()) return RESULT.PARAMETER_ERROR;
		
		ApplyItem db = get(item.getCode());
		
		if (null == db) return RESULT.NOT_FIND;
		
		boolean ok = false;
		
		if (null == db.getGn() || !db.getGn().equals(item.getGn())) {
			ok = true;
			db.setGn(item.getGn());
		}
		
		if (null == db.getCount() || (null != item.getCount() && db.getCount().intValue() != item.getCount().intValue())) {
			ok = true;
			db.setCount(item.getCount());
		}
		
		
		if (null == db.getPrice() || (null != item.getPrice() && db.getPrice().intValue() != item.getPrice().intValue())) {
			ok = true;
			db.setPrice(item.getPrice());
		}
		
		if (ok) 
			return super.upd(db);
		
		return RESULT.SUCCESS;
	}
	
}
